// pages/home/home.js
import {getMultiData, getGoodsData} from '../../service/home.js'
import {POP, NEW, SELL, BACK_TOP_POSITION} from '../../common/const.js'
const app = getApp()
Page({
  data: {
    banners: [],
    recommends: [],
    titles: ["流行", "新款", "精选"],
    goods:{
      [POP]: {page: 1, list: []},
      [NEW]: {page: 1, list: []},
      [SELL]: {page: 1, list: []}
    },
    currentType: 'pop',
    showBackTop: false,
    showTabControl: false,
    tabControlTop: 0,
    topPosition: 0,
    scriptHeight: 0
  },
  onLoad: function (options) {
    this.setData({
      scriptHeight: app.globalData.HEIGHT * 2
    })
    console.log(this.data.scriptHeight)
    //1.请求轮播图以及推荐数据
    this._getMultiData()
    //2.请求商品数据
    this._getGoodsData(POP)
    this._getGoodsData(SELL)
    this._getGoodsData(NEW)

  },
  /* ----------------事件监听函数----------------------------- */
  handleTabClick(event){
    //取出index
    const index = event.detail.index
    let currentType = ''
    switch(index){
      case 0:
        currentType = POP
        break
      case 1:
        currentType = NEW
        break
      case 2:
        currentType = SELL
        break
    }
    this.setData({
      currentType: currentType
    })
    console.log(this.selectComponent('.tab-control'));
    console.log(this.selectComponent('.tab-control-temp'));
    this.selectComponent('.tab-control').setCurrentIndex(index)
    this.selectComponent('.tab-control-temp').setCurrentIndex(index)
  },
  /* onReachBottom(){
    console.log('页面滚动到底部')
    //上拉加载更多，请求新的数据
    this._getGoodsData(this.data.currentType)
  }, */
  loadMore(){
    console.log('222222222222')
    //上拉加载更多，请求新的数据
    this._getGoodsData(this.data.currentType)
  },
  onImageLoad(){
    wx.createSelectorQuery().select('.tab-control').boundingClientRect(rect=>{
      this.setData({
        tabControlTop: rect.top
      })
    }).exec()
  },
  onBackTop(){
    // wx.pageScrollTo({
    //   scrollTop: 0,
    //   duration: 500
    // })
    this.setData({
      showBackTop: false,
      tabControlTop: 0,
      topPosition: 0
    })
  },
  scrollPosition(e){
    //1.获取滚动的顶部
    const position = e.detail.scrollTop
    //2.设置是否显示
    this.setData({
      showBackTop: position > BACK_TOP_POSITION
    })
    wx.createSelectorQuery().select('.tab-control').boundingClientRect(rect=>{
      const show = rect.top > 0
      this.setData({
        showTabControl: !show
      })
    }).exec()
  },

  /* ----------------网络请求函数---------------------------- */
  _getMultiData(){
    getMultiData().then(res=>{
      //取出轮播图喝推荐的数据
      const banners = res.data.banner.list.map(item => {
        return item.image
      });
      //将banners和recommends存放到data中
      this.setData({
        banners: banners,
        recommends: res.data.recommend.list
      })
    })
  },
  _getGoodsData(type){
    /* //1.获取页码
    const page = this.data.goods[type].page
    getGoodsData(type, page).then(res=>{
      //2.1.取出数据
      const list = res.data.list;
      console.log(list)
      //2.2.将数据设置到对应type的list中
      const oldList = this.data.goods[type].list
      oldList.push(...list)
      //2.3最新的goods设置到goods中
      const typeKey = `goods.${type}.list`
      const pageKey = `goods.${type}.page`
      this.setData({
        [typeKey]: oldList,
        [pageKey]: page
      })
    }) */

    // 1.获取数据对应的页码
    const page = this.data.goods[type].page;
    // 2.请求数据
    getGoodsData(type, page).then(res => {
      // 1.取出数据
      const list = res.data.list;
      // 2.将数据临时获取
      const goods = this.data.goods;
      goods[type].list.push(...list)
      goods[type].page += 1;

      // 3.最新的goods设置到goods中
      this.setData({
        goods: goods
      })
    })
  },
})